package com.lcf.app.jwt.controller;

import com.alibaba.fastjson.JSON;
import com.lcf.app.jwt.common.Current;
import com.lcf.app.jwt.common.CurrentUser;
import com.lcf.app.jwt.config.JwtHelper;
import com.lcf.app.jwt.model.User;
import com.lcf.common.web.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author ChenFei
 * @date 2022/1/18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class UserController {

    private final JwtHelper jwtHelper;

    @GetMapping("/login")
    public Result<String> login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        //效验登录用户和密码
        User user = new User()
                .setUsername(userName)
                .setId(UUID.randomUUID().toString())
                .setPassword(password);
        //生成token返回给用户
        return Result.success(jwtHelper.getToken(user));
    }

    @GetMapping("test")
    public Result<Object> test(@Current User user) {//1:参数获取当前用户信息
        log.info("user:{}", JSON.toJSONString(user));
        return Result.success(CurrentUser.get());//2：使用工具类获取当前信息
    }

}
